package cn.stylefeng.guns.modular.school.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.comm.util.CommConstant;
import cn.stylefeng.guns.modular.comm.model.result.NewsResult;
import cn.stylefeng.guns.modular.school.entity.ClassInfo;
import cn.stylefeng.guns.modular.school.model.params.ClassInfoParam;
import cn.stylefeng.guns.modular.school.model.result.ClassInfoResult;
import cn.stylefeng.guns.modular.school.model.result.SchoolInfoResult;
import cn.stylefeng.guns.modular.school.model.result.TeacherInfoResult;
import cn.stylefeng.guns.modular.school.service.ClassInfoService;
import cn.stylefeng.guns.modular.school.service.SchoolInfoService;
import cn.stylefeng.guns.modular.school.service.TeacherInfoService;
import cn.stylefeng.guns.sys.core.properties.GunsProperties;
import cn.stylefeng.guns.sys.modular.system.entity.FileInfo;
import cn.stylefeng.guns.sys.modular.system.service.FileInfoService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * 班级信息控制器
 *
 * @author
 * @Date 2019-11-25 09:46:38
 */
@Controller
@RequestMapping("/classInfo")
public class ClassInfoController extends BaseController {

    private String PREFIX = "/school/classInfo";
    @Autowired
    private GunsProperties gunsProperties;
    @Autowired
    private ClassInfoService classInfoService;
    @Autowired
    private SchoolInfoService schoolInfoService;
    @Autowired
    private TeacherInfoService teacherInfoService;
    @Autowired
    private FileInfoService fileInfoService;
    /**
     * 跳转到主页面
     *
     * @author
     * @Date 2019-11-25
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/classInfo.html";
    }

    /**
     * 新增页面
     *
     * @author
     * @Date 2019-11-25
     */
    @RequestMapping("/add")
    public String add() {
        init();
        return PREFIX + "/classInfo_add.html";
    }

    private void init() {
        // 学校
        List<SchoolInfoResult> schools = schoolInfoService.findListBySpec(null);
        setAttr("schools", schools);
        // 教师
        // TODO 先取得所有教师，以后有需求再改成根据学校取得本校的教师
        List<TeacherInfoResult> teachers = teacherInfoService.findListBySpec(null);
        teachers.forEach(teacher -> {
            teacher.setName(teacher.getCode() + "  " + teacher.getSexName() + "  " + teacher.getName());
        });
        setAttr("teachers", teachers);
    }

    /**
     * 编辑页面
     *
     * @author
     * @Date 2019-11-25
     */
    @RequestMapping("/edit")
    public String edit() {
        init();
        return PREFIX + "/classInfo_edit.html";
    }

    /**
     * 新增接口
     *
     * @author
     * @Date 2019-11-25
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(ClassInfoParam classInfoParam) {
        this.classInfoService.add(classInfoParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author
     * @Date 2019-11-25
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(ClassInfoParam classInfoParam) {
        this.classInfoService.update(classInfoParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author
     * @Date 2019-11-25
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(ClassInfoParam classInfoParam) {
        this.classInfoService.delete(classInfoParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author
     * @Date 2019-11-25
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(ClassInfoParam classInfoParam) {
        ClassInfo detail = this.classInfoService.getById(classInfoParam.getId());
        ClassInfoResult classInfoResult = new ClassInfoResult();
        ToolUtil.copyProperties(detail, classInfoResult);
        if (ToolUtil.isNotEmpty(detail.getFileId())) {
            FileInfo fileInfo = fileInfoService.getById(detail.getFileId());
            classInfoResult.setFilePath(CommConstant.FILE_PATH + fileInfo.getFilePath().substring(gunsProperties.getFileUploadPath().length() - 1));
        }
        return ResponseData.success(classInfoResult);
    }

    /**
     * 查询列表
     *
     * @author
     * @Date 2019-11-25
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(ClassInfoParam classInfoParam) {
        return this.classInfoService.findPageBySpec(classInfoParam);
    }

}


